package cn.com.zhbs.netty;

import cn.com.zhbs.netty.utils.ServerUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author 陈治杰
 * @Title: EchoServer
 * @Description: 将数据写入到通道中，用于将数据传给客户端
 * @date 2021/7/1 17:34
 */
public abstract class EchoServerSender {

    public void channelSend(ChannelHandlerContext ctx, int msg, byte[] body){
        byte[] s_data = new byte[body.length + 6];
        byte[] s_id= ServerUtil.intToByteArray(msg);
        byte[] s_length = ServerUtil.short2Byte((short) body.length);
        System.arraycopy(s_length, 0, s_data, 0, s_length.length);
        System.arraycopy(s_id, 0, s_data, 2, s_id.length);
        System.arraycopy(body, 0, s_data, 6, body.length);
        ByteBuf resp = Unpooled.copiedBuffer(s_data);
        ctx.write(resp);
    }

}
